﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class AccessRepo
    {
        public const string m_msg_head = "Access";

        public IEnumerable<AccessModels> GetAccess(string UserId)
        {
            List<AccessModels> accesss = new List<AccessModels>();
            string SQLCommand = @"  select TOP 20 *, TBVisitHD.LocationSetCode +'-'+TBLocationSet.LocSetNameTh AS locationSetCodeFullName,
                                         isnull(TBVisitHD.SerialNo,'')+'<'+isnull(TBModel.NameTh,'') +'>'+ isnull(TBMachine.NameTh,'') AS MachineFullName
                                   from TBVisitHD LEFT JOIN TBLocationSet
                                          ON TBVisitHD.LocationSetCode = TBLocationSet.LocationSetCode LEFT JOIN TBMachine
                                          ON TBVisitHD.SerialNo = TBMachine.SerialNo LEFT JOIN TBModel 
                                          ON TBMachine.ModelId = TBModel.ModelId
                                    WHERE TBVisitHD.Status <> 'Delete'
                                   And TBVisitHD.CreateBy=@UserId
                                    ORDER BY DateTimeIn DESC";
            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@UserId", UserId);
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        AccessModels access = new AccessModels();
                         
                        access.FSR_NO = (string)reader["FSR_NO"];
                        access.LocationSetCode = (string)reader["LocationSetCode"];
                        access.LocationSetCodeFullName = (string)reader["LocationSetCodeFullName"];
                        access.SerialNo = (string)reader["SerialNo"];
                        access.MachineFullName = (string)reader["MachineFullName"];
                        access.DateTimeIn = (DateTime)reader["DateTimeIn"];
                        access.DateTimeOut = (DateTime)reader["DateTimeOut"];
                        access.WorkType = (string)reader["WorkType"];
                        access.SignBy = (string)reader["SignBy"];
                        access.Status = (string)reader["Status"];
                        access.IsComplain = (bool)reader["IsComplain"];// == '1' ? true : false;

                        accesss.Add(access);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return accesss.ToList();
        }

        //dep 2,3
        public IEnumerable<AccessModels> GetAccess()
        {
            List<AccessModels> accesss = new List<AccessModels>();
            string SQLCommand = @"   select TOP 20 *, TBVisitHD.LocationSetCode +'-'+TBLocationSet.LocSetNameTh AS locationSetCodeFullName,
                                         isnull(TBVisitHD.SerialNo,'')+'<'+isnull(TBModel.NameTh,'') +'>'+ isnull(TBMachine.NameTh,'') AS MachineFullName
                                  from TBVisitHD LEFT JOIN TBLocationSet
                                          ON TBVisitHD.LocationSetCode = TBLocationSet.LocationSetCode LEFT JOIN TBMachine
                                          ON TBVisitHD.SerialNo = TBMachine.SerialNo LEFT JOIN TBModel 
                                          ON TBMachine.ModelId = TBModel.ModelId
                                 WHERE TBVisitHD.Status <> 'Delete'
                                 ORDER BY DateTimeIn DESC";
                                   //And TBVisitHD.CreateBy=@UserId";
            using (var db = new DBHelper())
            {
                try
                {
                   // db.AddParameter("@UserId", UserId);
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        AccessModels access = new AccessModels();

                        access.FSR_NO = (string)reader["FSR_NO"];
                        access.LocationSetCode = (string)reader["LocationSetCode"];
                        access.LocationSetCodeFullName = (string)reader["LocationSetCodeFullName"];
                        access.SerialNo = (string)reader["SerialNo"];
                        access.MachineFullName = (string)reader["MachineFullName"];
                        access.DateTimeIn = (DateTime)reader["DateTimeIn"];
                        access.DateTimeOut = (DateTime)reader["DateTimeOut"];
                        access.SignDatetime = (DateTime)reader["SignDatetime"];
                        access.WorkType = (string)reader["WorkType"];
                        access.SignBy = (string)reader["SignBy"];
                        access.Status = (string)reader["Status"];
                        access.IsComplain = (bool)reader["IsComplain"];// == '1' ? true : false;

                        accesss.Add(access);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return accesss.ToList();
        }
        public AccessModels GetVisitTemp(string UserId)
        {
            AccessModels access = new AccessModels();
            string SQLCommand = @"declare @Id int
                        select @Id=  max(id)  from TBVisitHDTemp where createBy=@UserId
                       select *,t1.LocationSetCode +'-'+t2.LocSetNameTh as                            locationSetCodeFullName  FROM TBVisitHDTemp t1 INNER JOIN TBLocationSet t2
                  ON t1.LocationSetCode =t2.LocationSetCode   
                        where t1.Id =@Id";
            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@UserId", UserId);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        access.Id = (int)reader["Id"];
                        access.SerialNo = (string)reader["SerialNo"];
                        access.LocationSetCode = (string)reader["LocationSetCode"];
                        access.LocationSetCodeFullName = (string)reader["locationSetCodeFullName"];
                        access.DateTimeIn = (DateTime)reader["DateTimeIn"];
                        access.Status = (string)reader["Status"];
                        access.ServiceBy = (string)reader["ServiceBy"];
                        access.CreateBy = (string)reader["CreateBy"];
                        access.CreateOn = (DateTime)reader["CreateOn"];
                        access.UpdateBy = (string)reader["UpdateBy"];
                        access.UpdateOn = (DateTime)reader["UpdateOn"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return access;
        }

        public bool Create(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int
                                    select @Id=isnull(max(id),0)+1 from TBVisitHDTemp 

INSERT into TBVisitHDTemp(Id,LocationSetCode,DateTimeIn,DateTimeOut,ServiceBy,Status,CreateBy,CreateOn,UpdateBy,UpdateOn)
                                VALUES(@Id,@LocationSetCode,getdate(),getdate(),@UserId,@Status,@UserId,getdate(),@UserId,getdate())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@LocationSetCode", access.LocationSetCode.Trim());
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID",access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool UpdateMachine(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int
                                    select @Id= max(id) from TBVisitHDTemp 
                                    where createby =@UserID
 
                                    update TBVisitHDTemp
                                    SET SerialNo=@SerialNo,
                                        WorkType=@WorkType,
	                                    UpdateBy=@UserId,
	                                    UpdateOn=GETDATE()	
                                    where Id =@Id ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@SerialNo", access.SerialNo.Trim());
                    db.AddParameter("@WorkType", access.WorkType);
                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateMeter(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"DECLARE @PreviousCount int
                                    declare @Id int   
                                    DECLARE @ConversionRate int 
                                    DECLARE @Rent decimal
                                    DECLARE @Discount int
                                    DECLARE @LocationSetCode varchar(10) 
                                    DECLARE @SerialNo varchar(20) 
 
 
                                    select @Id= max(id) from TBVisitHDTemp 
                                    where createby =@UserID 

                                     SELECT @LocationSetCode =LocationSetCode
                                     ,@SerialNo =SerialNo  from  tbvisithdtemp
                                     where Id =@Id

                                    SELECT @PreviousCount=last_meter_no from TBMachine where SerialNo = @SerialNo

                                    select @ConversionRate =ConversionRate ,
		                                    @Rent =RentBasic
		                                    FROM TBModel 
		                                    where ModelId =(select ModelId from TBMachine where SerialNo =@SerialNo)

                                    select  @Discount= CASE  WHEN DATEDIFF(YEAR,contract_start_date,GETDATE())<=5   then 0
			                                    WHEN DATEDIFF(YEAR,contract_start_date,GETDATE())>8 then 50
			                                    else 35 end    
                                     FROM TBLocationMachineSet 
                                     where LocationSetCode=@locationSetCode AND   @SerialNo =@SerialNo
 

                                    UPDATE TBVisitHDTemp
                                    SET PresentCount =@PresentCount,
                                        PreviousCount=@PreviousCount,
                                        TotalCount =@PresentCount-@PreviousCount,
                                        ConversionRate =@ConversionRate,
                                        TotalNoOfRacks =(@PresentCount-@PreviousCount)*@ConversionRate,
                                        Rent=@Rent,
                                        Discount =@Discount
                                    where Id =@Id
    

                                    --2
                                   -- Update TBMachine
                                   -- set last_meter_no =@PresentCount
                                    --where  SerialNo =@SerialNo

                                    --3
                                    --Update TBLocationMachineSet
                                    --set total_meter_value +=@PresentCount
                                   -- where LocationSetCode=@locationSetCode AND   SerialNo =@SerialNo";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@PresentCount", access.PresentCount);
                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateResultRemark(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlResultTemp where id=@Id

                    Insert Into TBVisitDtlResultTemp (Id,Seq,TitleResultName,SoilRemovalBit,
				                    FilmStreakBit,SpottedBit,ResultRemarkTh,ResultRemarkEng,
				                    Point)
                    values (@Id,@SeqI,@TitleResultNameI,@SoilRemovalBitI,
				                    @FilmStreakBitI,@SpottedBitI,@ResultRemarkThI,@ResultRemarkEngI,
				                    @PointI)

                     Insert Into TBVisitDtlResultTemp (Id,Seq,TitleResultName,SoilRemovalBit,
				                    FilmStreakBit,SpottedBit,ResultRemarkTh,ResultRemarkEng,
				                    Point)
                    values (@Id,@SeqII,@TitleResultNameII,@SoilRemovalBitII,
				                    @FilmStreakBitII,@SpottedBitII,@ResultRemarkThII,@ResultRemarkEngII,
				                    @PointII)

                     Insert Into TBVisitDtlResultTemp (Id,Seq,TitleResultName,SoilRemovalBit,
				                    FilmStreakBit,SpottedBit,ResultRemarkTh,ResultRemarkEng,
				                    Point)
                    values (@Id,@SeqIII,@TitleResultNameIII,@SoilRemovalBitIII,
				                    @FilmStreakBitIII,@SpottedBitIII,@ResultRemarkThIII,@ResultRemarkEngIII,
				                    @PointIII)
                     Insert Into TBVisitDtlResultTemp (Id,Seq,TitleResultName,SoilRemovalBit,
				                    FilmStreakBit,SpottedBit,ResultRemarkTh,ResultRemarkEng,
				                    Point)
                    values (@Id,@SeqIV,@TitleResultNameIV,@SoilRemovalBitIV,
				                    @FilmStreakBitIV,@SpottedBitIV,@ResultRemarkThIV,@ResultRemarkEngIV,
				                    @PointIV)
                     Insert Into TBVisitDtlResultTemp (Id,Seq,TitleResultName,SoilRemovalBit,
				                    FilmStreakBit,SpottedBit,ResultRemarkTh,ResultRemarkEng,
				                    Point)
                    values (@Id,@SeqV,@TitleResultNameV,@SoilRemovalBitV,
				                    @FilmStreakBitV,@SpottedBitV,@ResultRemarkThV,@ResultRemarkEngV,
				                    @PointV)
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI",1);
                    db.AddParameter("@TitleResultNameI", access.TitleResultName_I);
                    db.AddParameter("@SoilRemovalBitI", access.SoilRemovalBit_I);
                    db.AddParameter("@FilmStreakBitI", access.FilmStreakBit_I);
                    db.AddParameter("@SpottedBitI", access.SpottedBit_I);
                    db.AddParameter("@ResultRemarkThI", access.ResultRemarkTh_I);
                    db.AddParameter("@ResultRemarkEngI", access.ResultRemarkEng_I);
                    db.AddParameter("@PointI", access.ResultRemarkPointI);

                    db.AddParameter("@SeqII", 2);
                    db.AddParameter("@TitleResultNameII", access.TitleResultName_II);
                    db.AddParameter("@SoilRemovalBitII", access.SoilRemovalBit_II);
                    db.AddParameter("@FilmStreakBitII", access.FilmStreakBit_II);
                    db.AddParameter("@SpottedBitII", access.SpottedBit_II);
                    db.AddParameter("@ResultRemarkThII", access.ResultRemarkTh_II);
                    db.AddParameter("@ResultRemarkEngII", access.ResultRemarkEng_II);
                    db.AddParameter("@PointII", access.ResultRemarkPointII);

                    db.AddParameter("@SeqIII", 3);
                    db.AddParameter("@TitleResultNameIII", access.TitleResultName_III);
                    db.AddParameter("@SoilRemovalBitIII", access.SoilRemovalBit_III);
                    db.AddParameter("@FilmStreakBitIII", access.FilmStreakBit_III);
                    db.AddParameter("@SpottedBitIII", access.SpottedBit_III);
                    db.AddParameter("@ResultRemarkThIII", access.ResultRemarkTh_III);
                    db.AddParameter("@ResultRemarkEngIII", access.ResultRemarkEng_III);
                    db.AddParameter("@PointIII", access.ResultRemarkPointIII);

                    db.AddParameter("@SeqIV", 4);
                    db.AddParameter("@TitleResultNameIV", access.TitleResultName_IV);
                    db.AddParameter("@SoilRemovalBitIV", access.SoilRemovalBit_IV);
                    db.AddParameter("@FilmStreakBitIV", access.FilmStreakBit_IV);
                    db.AddParameter("@SpottedBitIV", access.SpottedBit_IV);
                    db.AddParameter("@ResultRemarkThIV", access.ResultRemarkTh_IV);
                    db.AddParameter("@ResultRemarkEngIV", access.ResultRemarkEng_IV);
                    db.AddParameter("@PointIV", access.ResultRemarkPointIV);

                    db.AddParameter("@SeqV", 5);
                    db.AddParameter("@TitleResultNameV", access.TitleResultName_V);
                    db.AddParameter("@SoilRemovalBitV", access.SoilRemovalBit_V);
                    db.AddParameter("@FilmStreakBitV", access.FilmStreakBit_V);
                    db.AddParameter("@SpottedBitV", access.SpottedBit_V);
                    db.AddParameter("@ResultRemarkThV", access.ResultRemarkTh_V);
                    db.AddParameter("@ResultRemarkEngV", access.ResultRemarkEng_V);
                    db.AddParameter("@PointV", access.ResultRemarkPointV);

                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateProcedure(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlProceduretemp where id=@Id
                                
                                insert into TBVisitDtlProceduretemp (Id,Seq,TitleProcudureName,
				                    OkBit,ProcedureRemarkTh,ProcedureRemarkEng,
				                    Point)
                               values (@Id,@SeqI,@TitleProcudureNameI,
				                    @OkBitI,@ProcedureRemarkThI,@ProcedureRemarkEngI,
				                    @PointI)

                                insert into TBVisitDtlProceduretemp (Id,Seq,TitleProcudureName,
				                    OkBit,ProcedureRemarkTh,ProcedureRemarkEng,
				                    Point)
                               values (@Id,@SeqII,@TitleProcudureNameII,
				                    @OkBitII,@ProcedureRemarkThII,@ProcedureRemarkEngII,
				                    @PointII)
                    
                                  insert into TBVisitDtlProceduretemp (Id,Seq,TitleProcudureName,
				                    OkBit,ProcedureRemarkTh,ProcedureRemarkEng,
				                    Point)
                               values (@Id,@SeqIII,@TitleProcudureNameIII,
				                    @OkBitIII,@ProcedureRemarkThIII,@ProcedureRemarkEngIII,
				                    @PointIII)

                             insert into TBVisitDtlProceduretemp (Id,Seq,TitleProcudureName,
				                    OkBit,ProcedureRemarkTh,ProcedureRemarkEng,
				                    Point)
                               values (@Id,@SeqIV,@TitleProcudureNameIV,
				                    @OkBitIV,@ProcedureRemarkThIV,@ProcedureRemarkEngIV,
				                    @PointIV)

                                 insert into TBVisitDtlProceduretemp (Id,Seq,TitleProcudureName,
				                    OkBit,ProcedureRemarkTh,ProcedureRemarkEng,
				                    Point)
                               values (@Id,@SeqV,@TitleProcudureNameV,
				                    @OkBitV,@ProcedureRemarkThV,@ProcedureRemarkEngV,
				                    @PointV)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleProcudureNameI", access.TitlePrecedureNameI);
                    db.AddParameter("@OkBitI", access.ProcedureCheckI);
                    db.AddParameter("@ProcedureRemarkThI", access.PredureRemarkThI);
                    db.AddParameter("@ProcedureRemarkEngI", access.PredureRemarkEnI);
                    db.AddParameter("@PointI", access.ProcedureRemarkPointI);

                    db.AddParameter("@SeqII", 2);
                    db.AddParameter("@TitleProcudureNameII", access.TitlePrecedureNameII);
                    db.AddParameter("@OkBitII", access.ProcedureCheckII);
                    db.AddParameter("@ProcedureRemarkThII", access.PredureRemarkThII);
                    db.AddParameter("@ProcedureRemarkEngII", access.PredureRemarkEnII);
                    db.AddParameter("@PointII", access.ProcedureRemarkPointII);

                    db.AddParameter("@SeqIII", 3);
                    db.AddParameter("@TitleProcudureNameIII", access.TitlePrecedureNameIII);
                    db.AddParameter("@OkBitIII", access.ProcedureCheckIII);
                    db.AddParameter("@ProcedureRemarkThIII", access.PredureRemarkThIII);
                    db.AddParameter("@ProcedureRemarkEngIII", access.PredureRemarkEnIII);
                    db.AddParameter("@PointIII", access.ProcedureRemarkPointIII);

                    db.AddParameter("@SeqIV",4);
                    db.AddParameter("@TitleProcudureNameIV", access.TitlePrecedureNameIV);
                    db.AddParameter("@OkBitIV", access.ProcedureCheckIV);
                    db.AddParameter("@ProcedureRemarkThIV", access.PredureRemarkThIV);
                    db.AddParameter("@ProcedureRemarkEngIV", access.PredureRemarkEnIV);
                    db.AddParameter("@PointIV", access.ProcedureRemarkPointIV);

                    db.AddParameter("@SeqV", 5);
                    db.AddParameter("@TitleProcudureNameV", access.TitlePrecedureNameV);
                    db.AddParameter("@OkBitV", access.ProcedureCheckV);
                    db.AddParameter("@ProcedureRemarkThV", access.PredureRemarkThV);
                    db.AddParameter("@ProcedureRemarkEngV", access.PredureRemarkEnV);
                    db.AddParameter("@PointV", access.ProcedureRemarkPointV);

                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateWashFactors(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlWashFactorTemp where id=@Id
                                
                                insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqI,@TitleWashFactorsNameI,
                                                        @ActualI,
                                                        @WashFactorRemarkThI,
                                                        @WashFactorRemarkEngI,
                                                        @PointI)


                                 insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqII,@TitleWashFactorsNameII,
                                                        @ActualII,
                                                        @WashFactorRemarkThII,
                                                        @WashFactorRemarkEngII,
                                                        @PointII)


                                insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqIII,@TitleWashFactorsNameIII,
                                                        @ActualIII,
                                                        @WashFactorRemarkThIII,
                                                        @WashFactorRemarkEngIII,
                                                        @PointIII)

                                insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqIV,@TitleWashFactorsNameIV,
                                                        @ActualIV,
                                                        @WashFactorRemarkThIV,
                                                        @WashFactorRemarkEngIV,
                                                        @PointIV)

                            insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqV,@TitleWashFactorsNameV,
                                                        @ActualV,
                                                        @WashFactorRemarkThV,
                                                        @WashFactorRemarkEngV,
                                                        @PointV)

                            insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqVI,@TitleWashFactorsNameVI,
                                                        @ActualVI,
                                                        @WashFactorRemarkThVI,
                                                        @WashFactorRemarkEngVI,
                                                        @PointVI)

                             insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqVII,@TitleWashFactorsNameVII,
                                                        @ActualVII,
                                                        @WashFactorRemarkThVII,
                                                        @WashFactorRemarkEngVII,
                                                        @PointVII)

                            insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqVIII,@TitleWashFactorsNameVIII,
                                                        @ActualVIII,
                                                        @WashFactorRemarkThVIII,
                                                        @WashFactorRemarkEngVIII,
                                                        @PointVIII)

                            insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqIX,@TitleWashFactorsNameIX,
                                                        @ActualIX,
                                                        @WashFactorRemarkThIX,
                                                        @WashFactorRemarkEngIX,
                                                        @PointIX)

                            insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqX,@TitleWashFactorsNameX,
                                                        @ActualX,
                                                        @WashFactorRemarkThX,
                                                        @WashFactorRemarkEngX,
                                                        @PointX)

                             insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqXI,@TitleWashFactorsNameXI,
                                                        @ActualXI,
                                                        @WashFactorRemarkThXI,
                                                        @WashFactorRemarkEngXI,
                                                        @PointXI)

                            insert into TBVisitDtlWashFactorTemp (Id,Seq,TitleWashFactorsName,
                                                        Actual,
                                                        WashFactorRemarkTh,
                                                        WashFactorRemarkEng,
                                                        Point)
                               values (@Id,@SeqXII,@TitleWashFactorsNameXII,
                                                        @ActualXII,
                                                        @WashFactorRemarkThXII,
                                                        @WashFactorRemarkEngXII,
                                                        @PointXII)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleWashFactorsNameI", access.TitleWashFactorNameI);
                    db.AddParameter("@ActualI", access.ActualI);
                    db.AddParameter("@WashFactorRemarkThI", access.WashFactorRemarkThI);
                    db.AddParameter("@WashFactorRemarkEngI", access.WashFactorRemarkEnI);
                    db.AddParameter("@PointI", access.ActualPointI);

                    db.AddParameter("@SeqII", 2);
                    db.AddParameter("@TitleWashFactorsNameII", access.TitleWashFactorNameII);
                    db.AddParameter("@ActualII", access.ActualII);
                    db.AddParameter("@WashFactorRemarkThII", access.WashFactorRemarkThII);
                    db.AddParameter("@WashFactorRemarkEngII", access.WashFactorRemarkEnII);
                    db.AddParameter("@PointII", access.ActualPointII);

                    db.AddParameter("@SeqIII", 3);
                    db.AddParameter("@TitleWashFactorsNameIII", access.TitleWashFactorNameIII);
                    db.AddParameter("@ActualIII", access.ActualIII);
                    db.AddParameter("@WashFactorRemarkThIII", access.WashFactorRemarkThIII);
                    db.AddParameter("@WashFactorRemarkEngIII", access.WashFactorRemarkEnIII);
                    db.AddParameter("@PointIII", access.ActualPointIII);

                    db.AddParameter("@SeqIV", 4);
                    db.AddParameter("@TitleWashFactorsNameIV", access.TitleWashFactorNameIV);
                    db.AddParameter("@ActualIV", access.ActualIV);
                    db.AddParameter("@WashFactorRemarkThIV", access.WashFactorRemarkThIV);
                    db.AddParameter("@WashFactorRemarkEngIV", access.WashFactorRemarkEnIV);
                    db.AddParameter("@PointIV", access.ActualPointIV);

                    db.AddParameter("@SeqV", 5);
                    db.AddParameter("@TitleWashFactorsNameV", access.TitleWashFactorNameV);
                    db.AddParameter("@ActualV", access.ActualV);
                    db.AddParameter("@WashFactorRemarkThV", access.WashFactorRemarkThV);
                    db.AddParameter("@WashFactorRemarkEngV", access.WashFactorRemarkEnV);
                    db.AddParameter("@PointV", access.ActualPointV);

                    db.AddParameter("@SeqVI", 6);
                    db.AddParameter("@TitleWashFactorsNameVI", access.TitleWashFactorNameVI);
                    db.AddParameter("@ActualVI", access.ActualVI);
                    db.AddParameter("@WashFactorRemarkThVI", access.WashFactorRemarkThVI);
                    db.AddParameter("@WashFactorRemarkEngVI", access.WashFactorRemarkEnVI);
                    db.AddParameter("@PointVI", access.ActualPointVI);

                    db.AddParameter("@SeqVII", 7);
                    db.AddParameter("@TitleWashFactorsNameVII", access.TitleWashFactorNameVII);
                    db.AddParameter("@ActualVII", access.ActualVII);
                    db.AddParameter("@WashFactorRemarkThVII", access.WashFactorRemarkThVII);
                    db.AddParameter("@WashFactorRemarkEngVII", access.WashFactorRemarkEnVII);
                    db.AddParameter("@PointVII", access.ActualPointVII);

                    db.AddParameter("@SeqVIII", 8);
                    db.AddParameter("@TitleWashFactorsNameVIII", access.TitleWashFactorNameVIII);
                    db.AddParameter("@ActualVIII", access.ActualVIII);
                    db.AddParameter("@WashFactorRemarkThVIII", access.WashFactorRemarkThVIII);
                    db.AddParameter("@WashFactorRemarkEngVIII", access.WashFactorRemarkEnVIII);
                    db.AddParameter("@PointVIII", access.ActualPointVIII);

                    db.AddParameter("@SeqIX", 9);
                    db.AddParameter("@TitleWashFactorsNameIX", access.TitleWashFactorNameIX);
                    db.AddParameter("@ActualIX", access.ActualIX);
                    db.AddParameter("@WashFactorRemarkThIX", access.WashFactorRemarkThIX);
                    db.AddParameter("@WashFactorRemarkEngIX", access.WashFactorRemarkEnIX);
                    db.AddParameter("@PointIX", access.ActualPointIX);

                    db.AddParameter("@SeqX", 10);
                    db.AddParameter("@TitleWashFactorsNameX", access.TitleWashFactorNameX);
                    db.AddParameter("@ActualX", access.ActualX);
                    db.AddParameter("@WashFactorRemarkThX", access.WashFactorRemarkThX);
                    db.AddParameter("@WashFactorRemarkEngX", access.WashFactorRemarkEnX);
                    db.AddParameter("@PointX", access.ActualPointX);

                    db.AddParameter("@SeqXI", 11);
                    db.AddParameter("@TitleWashFactorsNameXI", access.TitleWashFactorNameXI);
                    db.AddParameter("@ActualXI", access.ActualXI);
                    db.AddParameter("@WashFactorRemarkThXI", access.WashFactorRemarkThXI);
                    db.AddParameter("@WashFactorRemarkEngXI", access.WashFactorRemarkEnXI);
                    db.AddParameter("@PointXI", access.ActualPointXI);

                    db.AddParameter("@SeqXII", 12);
                    db.AddParameter("@TitleWashFactorsNameXII", access.TitleWashFactorNameXII);
                    db.AddParameter("@ActualXII", access.ActualXII);
                    db.AddParameter("@WashFactorRemarkThXII", access.WashFactorRemarkThXII);
                    db.AddParameter("@WashFactorRemarkEngXII", access.WashFactorRemarkEnXII);
                    db.AddParameter("@PointXII", access.ActualPointXII);

                    db.AddParameter("@UserID",access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateMECHANICAL(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlMechanicalTemp where id=@Id
                                
                                insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqI,
                                                        @TitleMechanicalNameI,
                                                        @OkBitI,
                                                        @MechanicalRemarkThI,
                                                        @MechanicalRemarkEngI,
                                                        @PointI)


                                insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqII,
                                                        @TitleMechanicalNameII,
                                                        @OkBitII,
                                                        @MechanicalRemarkThII,
                                                        @MechanicalRemarkEngII,
                                                        @PointII)

                                insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqIII,
                                                        @TitleMechanicalNameIII,
                                                        @OkBitIII,
                                                        @MechanicalRemarkThIII,
                                                        @MechanicalRemarkEngIII,
                                                        @PointIII)

                            insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqIV,
                                                        @TitleMechanicalNameIV,
                                                        @OkBitIV,
                                                        @MechanicalRemarkThIV,
                                                        @MechanicalRemarkEngIV,
                                                        @PointIV)

                            insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqV,
                                                        @TitleMechanicalNameV,
                                                        @OkBitV,
                                                        @MechanicalRemarkThV,
                                                        @MechanicalRemarkEngV,
                                                        @PointV)

                             insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqVI,
                                                        @TitleMechanicalNameVI,
                                                        @OkBitVI,
                                                        @MechanicalRemarkThVI,
                                                        @MechanicalRemarkEngVI,
                                                        @PointVI)

                            insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqVII,
                                                        @TitleMechanicalNameVII,
                                                        @OkBitVII,
                                                        @MechanicalRemarkThVII,
                                                        @MechanicalRemarkEngVII,
                                                        @PointVII)

                            insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqVIII,
                                                        @TitleMechanicalNameVIII,
                                                        @OkBitVIII,
                                                        @MechanicalRemarkThVIII,
                                                        @MechanicalRemarkEngVIII,
                                                        @PointVIII)

                            insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqIX,
                                                        @TitleMechanicalNameIX,
                                                        @OkBitIX,
                                                        @MechanicalRemarkThIX,
                                                        @MechanicalRemarkEngIX,
                                                        @PointIX)

                             insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqX,
                                                        @TitleMechanicalNameX,
                                                        @OkBitX,
                                                        @MechanicalRemarkThX,
                                                        @MechanicalRemarkEngX,
                                                        @PointX)


                                 insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqXI,
                                                        @TitleMechanicalNameXI,
                                                        @OkBitXI,
                                                        @MechanicalRemarkThXI,
                                                        @MechanicalRemarkEngXI,
                                                        @PointXI)

                                insert into TBVisitDtlMechanicalTemp (Id,Seq,
                                                        TitleMechanicalName,
                                                        OkBit,
                                                        MechanicalRemarkTh,
                                                        MechanicalRemarkEng,
                                                        Point)
                                                values (@Id,@SeqXII,
                                                        @TitleMechanicalNameXII,
                                                        @OkBitXII,
                                                        @MechanicalRemarkThXII,
                                                        @MechanicalRemarkEngXII,
                                                        @PointXII)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleMechanicalNameI", access.TitleMechanicalNameI);
                    db.AddParameter("@OkBitI", access.MechanicalCheckI);
                    db.AddParameter("@MechanicalRemarkThI", access.MechanicalRemarkThI);
                    db.AddParameter("@MechanicalRemarkEngI", access.MechanicalRemarkEnI);
                    db.AddParameter("@PointI", access.MechanicalPointI);

                    db.AddParameter("@SeqII", 2);
                    db.AddParameter("@TitleMechanicalNameII", access.TitleMechanicalNameII);
                    db.AddParameter("@OkBitII", access.MechanicalCheckII);
                    db.AddParameter("@MechanicalRemarkThII", access.MechanicalRemarkThII);
                    db.AddParameter("@MechanicalRemarkEngII", access.MechanicalRemarkEnII);
                    db.AddParameter("@PointII", access.MechanicalPointII);

                    db.AddParameter("@SeqIII", 3);
                    db.AddParameter("@TitleMechanicalNameIII", access.TitleMechanicalNameIII);
                    db.AddParameter("@OkBitIII", access.MechanicalCheckIII);
                    db.AddParameter("@MechanicalRemarkThIII", access.MechanicalRemarkThIII);
                    db.AddParameter("@MechanicalRemarkEngIII", access.MechanicalRemarkEnIII);
                    db.AddParameter("@PointIII", access.MechanicalPointIII);

                    db.AddParameter("@SeqIV", 4);
                    db.AddParameter("@TitleMechanicalNameIV", access.TitleMechanicalNameIV);
                    db.AddParameter("@OkBitIV", access.MechanicalCheckIV);
                    db.AddParameter("@MechanicalRemarkThIV", access.MechanicalRemarkThIV);
                    db.AddParameter("@MechanicalRemarkEngIV", access.MechanicalRemarkEnIV);
                    db.AddParameter("@PointIV", access.MechanicalPointIV);

                    db.AddParameter("@SeqV", 5);
                    db.AddParameter("@TitleMechanicalNameV", access.TitleMechanicalNameV);
                    db.AddParameter("@OkBitV", access.MechanicalCheckV);
                    db.AddParameter("@MechanicalRemarkThV", access.MechanicalRemarkThV);
                    db.AddParameter("@MechanicalRemarkEngV", access.MechanicalRemarkEnV);
                    db.AddParameter("@PointV", access.MechanicalPointV);

                    db.AddParameter("@SeqVI", 6);
                    db.AddParameter("@TitleMechanicalNameVI", access.TitleMechanicalNameVI);
                    db.AddParameter("@OkBitVI", access.MechanicalCheckVI);
                    db.AddParameter("@MechanicalRemarkThVI", access.MechanicalRemarkThVI);
                    db.AddParameter("@MechanicalRemarkEngVI", access.MechanicalRemarkEnVI);
                    db.AddParameter("@PointVI", access.MechanicalPointVI);

                    db.AddParameter("@SeqVII", 7);
                    db.AddParameter("@TitleMechanicalNameVII", access.TitleMechanicalNameVII);
                    db.AddParameter("@OkBitVII", access.MechanicalCheckVII);
                    db.AddParameter("@MechanicalRemarkThVII", access.MechanicalRemarkThVII);
                    db.AddParameter("@MechanicalRemarkEngVII", access.MechanicalRemarkEnVII);
                    db.AddParameter("@PointVII", access.MechanicalPointVII);

                    db.AddParameter("@SeqVIII", 8);
                    db.AddParameter("@TitleMechanicalNameVIII", access.TitleMechanicalNameVIII);
                    db.AddParameter("@OkBitVIII", access.MechanicalCheckVIII);
                    db.AddParameter("@MechanicalRemarkThVIII", access.MechanicalRemarkThVIII);
                    db.AddParameter("@MechanicalRemarkEngVIII", access.MechanicalRemarkEnVIII);
                    db.AddParameter("@PointVIII", access.MechanicalPointVIII);

                    db.AddParameter("@SeqIX", 9);
                    db.AddParameter("@TitleMechanicalNameIX", access.TitleMechanicalNameIX);
                    db.AddParameter("@OkBitIX", access.MechanicalCheckIX);
                    db.AddParameter("@MechanicalRemarkThIX", access.MechanicalRemarkThIX);
                    db.AddParameter("@MechanicalRemarkEngIX", access.MechanicalRemarkEnIX);
                    db.AddParameter("@PointIX", access.MechanicalPointIX);

                    db.AddParameter("@SeqX", 10);
                    db.AddParameter("@TitleMechanicalNameX", access.TitleMechanicalNameX);
                    db.AddParameter("@OkBitX", access.MechanicalCheckX);
                    db.AddParameter("@MechanicalRemarkThX", access.MechanicalRemarkThX);
                    db.AddParameter("@MechanicalRemarkEngX", access.MechanicalRemarkEnX);
                    db.AddParameter("@PointX", access.MechanicalPointX);

                    db.AddParameter("@SeqXI", 11);
                    db.AddParameter("@TitleMechanicalNameXI", access.TitleMechanicalNameXI);
                    db.AddParameter("@OkBitXI", access.MechanicalCheckXI);
                    db.AddParameter("@MechanicalRemarkThXI", access.MechanicalRemarkThXI);
                    db.AddParameter("@MechanicalRemarkEngXI", access.MechanicalRemarkEnXI);
                    db.AddParameter("@PointXI", access.MechanicalPointXI);

                    db.AddParameter("@SeqXII", 12);
                    db.AddParameter("@TitleMechanicalNameXII", access.TitleMechanicalNameXII);
                    db.AddParameter("@OkBitXII", access.MechanicalCheckXII);
                    db.AddParameter("@MechanicalRemarkThXII", access.MechanicalRemarkThXII);
                    db.AddParameter("@MechanicalRemarkEngXII", access.MechanicalRemarkEnXII);
                    db.AddParameter("@PointXII", access.MechanicalPointXII);


                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateMWWDispenser(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlMwwDispenserTemp where id=@Id
                                
                                insert into TBVisitDtlMwwDispenserTemp (Id,Seq,
										TitleMwwDispenserName,
										ProbeBit,
										DetTubeBit,
										RinseTubeBit,
										ConditionRemarkTh,
										ConditionRemarkEng,
										Point)
			                  values	(@Id,@SeqI,
										@TitleMwwDispenserNameI,
										@ProbeBitI,
										@DetTubeBitI,
										@RinseTubeBitI,
										@ConditionRemarkThI,
										@ConditionRemarkEngI,
										@PointI)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleMwwDispenserNameI", access.ConditionMachineI);
                    db.AddParameter("@ProbeBitI", access.ConditionProbeCheckI);
                    db.AddParameter("@DetTubeBitI", access.ConditionDettubeCheckI);
                    db.AddParameter("@RinseTubeBitI", access.ConditionRinsetubeCheckI);
                    db.AddParameter("@ConditionRemarkThI", access.ConditionRemarkThI);
                    db.AddParameter("@ConditionRemarkEngI", access.ConditionRemarkEnI);
                    db.AddParameter("@PointI", access.ConditionRemarkPointI);

                    db.AddParameter("@UserID",access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateFeeder(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlFeederTemp where id=@Id
                                
                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqI,
                                        @TitleFeeDerNameI,
                                        @FeederRemarkThI,
                                        @FeederRemarkEngI,
                                        @StandardI,
                                        @ActualI,
                                        @PointI)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqII,
                                        @TitleFeeDerNameII,
                                        @FeederRemarkThII,
                                        @FeederRemarkEngII,
                                        @StandardII,
                                        @ActualII,
                                        @PointII)

                              insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqIII,
                                        @TitleFeeDerNameIII,
                                        @FeederRemarkThIII,
                                        @FeederRemarkEngIII,
                                        @StandardIII,
                                        @ActualIII,
                                        @PointIII)

                            insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqIV,
                                        @TitleFeeDerNameIV,
                                        @FeederRemarkThIV,
                                        @FeederRemarkEngIV,
                                        @StandardIV,
                                        @ActualIV,
                                        @PointIV)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqV,
                                        @TitleFeeDerNameV,
                                        @FeederRemarkThV,
                                        @FeederRemarkEngV,
                                        @StandardV,
                                        @ActualV,
                                        @PointV)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqVI,
                                        @TitleFeeDerNameVI,
                                        @FeederRemarkThVI,
                                        @FeederRemarkEngVI,
                                        @StandardVI,
                                        @ActualVI,
                                        @PointVI)

                            insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqVII,
                                        @TitleFeeDerNameVII,
                                        @FeederRemarkThVII,
                                        @FeederRemarkEngVII,
                                        @StandardVII,
                                        @ActualVII,
                                        @PointVII)

                            insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqVIII,
                                        @TitleFeeDerNameVIII,
                                        @FeederRemarkThVIII,
                                        @FeederRemarkEngVIII,
                                        @StandardVIII,
                                        @ActualVIII,
                                        @PointVIII)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqIX,
                                        @TitleFeeDerNameIX,
                                        @FeederRemarkThIX,
                                        @FeederRemarkEngIX,
                                        @StandardIX,
                                        @ActualIX,
                                        @PointIX)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqX,
                                        @TitleFeeDerNameX,
                                        @FeederRemarkThX,
                                        @FeederRemarkEngX,
                                        @StandardX,
                                        @ActualX,
                                        @PointX)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqXI,
                                        @TitleFeeDerNameXI,
                                        @FeederRemarkThXI,
                                        @FeederRemarkEngXI,
                                        @StandardXI,
                                        @ActualXI,
                                        @PointXI)

                               insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqXII,
                                        @TitleFeeDerNameXII,
                                        @FeederRemarkThXII,
                                        @FeederRemarkEngXII,
                                        @StandardXII,
                                        @ActualXII,
                                        @PointXII)

                                insert into TBVisitDtlFeederTemp (Id,Seq,
                                        TitleFeeDerName,
                                        FeederRemarkTh,
                                        FeederRemarkEng,
                                        [Standard],
                                        Actual,
                                        Point)
			                   values	(@Id,@SeqXIII,
                                        @TitleFeeDerNameXIII,
                                        @FeederRemarkThXIII,
                                        @FeederRemarkEngXIII,
                                        @StandardXIII,
                                        @ActualXIII,
                                        @PointXIII)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleFeeDerNameI", access.TitleFeederI);
                    db.AddParameter("@FeederRemarkThI", access.FeederRemarkThI);
                    db.AddParameter("@FeederRemarkEngI", access.FeederRemarkEngI);
                    db.AddParameter("@StandardI", access.FeederStandardI);
                    db.AddParameter("@ActualI", access.FeederActualI);
                    db.AddParameter("@PointI", access.FeederPointI);

                    db.AddParameter("@SeqII", 2);
                    db.AddParameter("@TitleFeeDerNameII", access.TitleFeederII);
                    db.AddParameter("@FeederRemarkThII", access.FeederRemarkThII);
                    db.AddParameter("@FeederRemarkEngII", access.FeederRemarkEngII);
                    db.AddParameter("@StandardII", access.FeederStandardII);
                    db.AddParameter("@ActualII", access.FeederActualII);
                    db.AddParameter("@PointII", access.FeederPointII);

                    db.AddParameter("@SeqIII", 3);
                    db.AddParameter("@TitleFeeDerNameIII", access.TitleFeederIII);
                    db.AddParameter("@FeederRemarkThIII", access.FeederRemarkThIII);
                    db.AddParameter("@FeederRemarkEngIII", access.FeederRemarkEngIII);
                    db.AddParameter("@StandardIII", access.FeederStandardIII);
                    db.AddParameter("@ActualIII", access.FeederActualIII);
                    db.AddParameter("@PointIII", access.FeederPointIII);

                    db.AddParameter("@SeqIV", 4);
                    db.AddParameter("@TitleFeeDerNameIV", access.TitleFeederIV);
                    db.AddParameter("@FeederRemarkThIV", access.FeederRemarkThIV);
                    db.AddParameter("@FeederRemarkEngIV", access.FeederRemarkEngIV);
                    db.AddParameter("@StandardIV", access.FeederStandardIV);
                    db.AddParameter("@ActualIV", access.FeederActualIV);
                    db.AddParameter("@PointIV", access.FeederPointIV);

                    db.AddParameter("@SeqV", 5);
                    db.AddParameter("@TitleFeeDerNameV", access.TitleFeederV);
                    db.AddParameter("@FeederRemarkThV", access.FeederRemarkThV);
                    db.AddParameter("@FeederRemarkEngV", access.FeederRemarkEngV);
                    db.AddParameter("@StandardV", access.FeederStandardV);
                    db.AddParameter("@ActualV", access.FeederActualV);
                    db.AddParameter("@PointV", access.FeederPointV);

                    db.AddParameter("@SeqVI", 6);
                    db.AddParameter("@TitleFeeDerNameVI", access.TitleFeederVI);
                    db.AddParameter("@FeederRemarkThVI", access.FeederRemarkThVI);
                    db.AddParameter("@FeederRemarkEngVI", access.FeederRemarkEngVI);
                    db.AddParameter("@StandardVI", access.FeederStandardVI);
                    db.AddParameter("@ActualVI", access.FeederActualVI);
                    db.AddParameter("@PointVI", access.FeederPointVI);

                    db.AddParameter("@SeqVII", 7);
                    db.AddParameter("@TitleFeeDerNameVII", access.TitleFeederVII);
                    db.AddParameter("@FeederRemarkThVII", access.FeederRemarkThVII);
                    db.AddParameter("@FeederRemarkEngVII", access.FeederRemarkEngVII);
                    db.AddParameter("@StandardVII", access.FeederStandardVII);
                    db.AddParameter("@ActualVII", access.FeederActualVII);
                    db.AddParameter("@PointVII", access.FeederPointVII);

                    db.AddParameter("@SeqVIII", 8);
                    db.AddParameter("@TitleFeeDerNameVIII", access.TitleFeederVIII);
                    db.AddParameter("@FeederRemarkThVIII", access.FeederRemarkThVIII);
                    db.AddParameter("@FeederRemarkEngVIII", access.FeederRemarkEngVIII);
                    db.AddParameter("@StandardVIII", access.FeederStandardVIII);
                    db.AddParameter("@ActualVIII", access.FeederActualVIII);
                    db.AddParameter("@PointVIII", access.FeederPointVIII);

                    db.AddParameter("@SeqIX", 9);
                    db.AddParameter("@TitleFeeDerNameIX", access.TitleFeederIX);
                    db.AddParameter("@FeederRemarkThIX", access.FeederRemarkThIX);
                    db.AddParameter("@FeederRemarkEngIX", access.FeederRemarkEngIX);
                    db.AddParameter("@StandardIX", access.FeederStandardIX);
                    db.AddParameter("@ActualIX", access.FeederActualIX);
                    db.AddParameter("@PointIX", access.FeederPointIX);

                    db.AddParameter("@SeqX", 10);
                    db.AddParameter("@TitleFeeDerNameX", access.TitleFeederX);
                    db.AddParameter("@FeederRemarkThX", access.FeederRemarkThX);
                    db.AddParameter("@FeederRemarkEngX", access.FeederRemarkEngX);
                    db.AddParameter("@StandardX", access.FeederStandardX);
                    db.AddParameter("@ActualX", access.FeederActualX);
                    db.AddParameter("@PointX", access.FeederPointX);

                    db.AddParameter("@SeqXI", 11);
                    db.AddParameter("@TitleFeeDerNameXI", access.TitleFeederXI);
                    db.AddParameter("@FeederRemarkThXI", access.FeederRemarkThXI);
                    db.AddParameter("@FeederRemarkEngXI", access.FeederRemarkEngXI);
                    db.AddParameter("@StandardXI", access.FeederStandardXI);
                    db.AddParameter("@ActualXI", access.FeederActualXI);
                    db.AddParameter("@PointXI", access.FeederPointXI);

                    db.AddParameter("@SeqXII", 12);
                    db.AddParameter("@TitleFeeDerNameXII", access.TitleFeederXII);
                    db.AddParameter("@FeederRemarkThXII", access.FeederRemarkThXII);
                    db.AddParameter("@FeederRemarkEngXII", access.FeederRemarkEngXII);
                    db.AddParameter("@StandardXII", access.FeederStandardXII);
                    db.AddParameter("@ActualXII", access.FeederActualXII);
                    db.AddParameter("@PointXII", access.FeederPointXII);

                    db.AddParameter("@SeqXIII", 13);
                    db.AddParameter("@TitleFeeDerNameXIII", access.TitleFeederXIII);
                    db.AddParameter("@FeederRemarkThXIII", access.FeederRemarkThXIII);
                    db.AddParameter("@FeederRemarkEngXIII", access.FeederRemarkEngXIII);
                    db.AddParameter("@StandardXIII", access.FeederStandardXIII);
                    db.AddParameter("@ActualXIII", access.FeederActualXIII);
                    db.AddParameter("@PointXIII", access.FeederPointXIII);
 

                    db.AddParameter("@UserID",access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateTraining(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlTrainingTemp where id=@Id
                                
                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqI,
                                        @TitleOnsiteTraningNameI,
                                        @OkBitI)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqII,
                                        @TitleOnsiteTraningNameII,
                                        @OkBitII)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqIII,
                                        @TitleOnsiteTraningNameIII,
                                        @OkBitIII)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqIV,
                                        @TitleOnsiteTraningNameIV,
                                        @OkBitIV)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqV,
                                        @TitleOnsiteTraningNameV,
                                        @OkBitV)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqVI,
                                        @TitleOnsiteTraningNameVI,
                                        @OkBitVI)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqVII,
                                        @TitleOnsiteTraningNameVII,
                                        @OkBitVII)

                                insert into TBVisitDtlTrainingTemp (Id,Seq,
                                        TitleOnsiteTraningName,
                                        OkBit)
			                    values	(@Id,@SeqVIII,
                                        @TitleOnsiteTraningNameVIII,
                                        @OkBitVIII)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleOnsiteTraningNameI", access.TitleTraningI);
                    db.AddParameter("@OkBitI", access.TreningICheck);

                    db.AddParameter("@SeqII", 2);
                    db.AddParameter("@TitleOnsiteTraningNameII", access.TitleTraningII);
                    db.AddParameter("@OkBitII", access.TreningIICheck);

                    db.AddParameter("@SeqIII", 3);
                    db.AddParameter("@TitleOnsiteTraningNameIII", access.TitleTraningIII);
                    db.AddParameter("@OkBitIII", access.TreningIIICheck);

                    db.AddParameter("@SeqIV", 4);
                    db.AddParameter("@TitleOnsiteTraningNameIV", access.TitleTraningIV);
                    db.AddParameter("@OkBitIV", access.TreningIVCheck);

                    db.AddParameter("@SeqV", 5);
                    db.AddParameter("@TitleOnsiteTraningNameV", access.TitleTraningV);
                    db.AddParameter("@OkBitV", access.TreningVCheck);

                    db.AddParameter("@SeqVI", 6);
                    db.AddParameter("@TitleOnsiteTraningNameVI", access.TitleTraningVI);
                    db.AddParameter("@OkBitVI", access.TreningVICheck);

                    db.AddParameter("@SeqVII", 7);
                    db.AddParameter("@TitleOnsiteTraningNameVII", access.TitleTraningVII);
                    db.AddParameter("@OkBitVII", access.TreningVIICheck);

                    db.AddParameter("@SeqVIII", 8);
                    db.AddParameter("@TitleOnsiteTraningNameVIII", access.TitleTraningVIII);
                    db.AddParameter("@OkBitVIII", access.TreningVIIICheck);
                 

                    db.AddParameter("@UserID",access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool UpdateHandSoapDispenser(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"declare @Id int 
                                    select @Id =max(Id) from TBVisitHDTemp where CreateBy=@UserID
                                    and Status ='Active'
                                    delete from  TBVisitDtlHandSoapDispenserTemp where id=@Id
                                
                                insert into TBVisitDtlHandSoapDispenserTemp (Id,Seq,
                                        TitleHandSoapDispenserName,
                                        Kitchen_Bit,
                                        HandSoapDispenserRemarkTh,
                                        HandSoapDispenserRemarkEng,
                                        Point)
			                  values	(@Id,@SeqI,
                                        @TitleHandSoapDispenserNameI,
                                        @Kitchen_BitI,
                                        @HandSoapDispenserRemarkThI,
                                        @HandSoapDispenserRemarkEngI,
                                        @PointI)
                   
                        ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();

                    db.AddParameter("@SeqI", 1);
                    db.AddParameter("@TitleHandSoapDispenserNameI", access.TitleHandSoapDispenser);
                    db.AddParameter("@Kitchen_BitI", access.HandSoapDispenserCheck);
                    db.AddParameter("@HandSoapDispenserRemarkThI", access.HandSoapDispenserThRemarksI);
                    db.AddParameter("@HandSoapDispenserRemarkEngI", access.HandSoapDispenserEngRemarksI);
                    db.AddParameter("@PointI", access.HandSoapDispenserRemarkPointI);

                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        public bool Finish(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"  declare @Id int
                                    declare @FSR varchar(13)
                                    DECLARE @PresentCount int
                                      DECLARE @LocationSetCode varchar(10) 
                                     DECLARE @SerialNo varchar(20) 

                                    select @Id= max(id) from TBVisitHDTemp 
                                    where createby =@UserID
 
                                    
                                    update TBVisitHDTemp set comment =@Comment,DateTimeOut=Getdate() 
                                    where Id=@Id

                                                                       
                                      SELECT @LocationSetCode =LocationSetCode
                                     ,@SerialNo =SerialNo ,
                                    @PresentCount=PresentCount
                                    from  tbvisithdtemp
                                     where Id =@Id

    
                                      --2
                                    Update TBMachine
                                   set last_meter_no =@PresentCount
                                    where  SerialNo =@SerialNo

                                    --3
                                    Update TBLocationMachineSet
                                    set last_meter_no = @PresentCount,
                                        total_meter_value =total_meter_value + @PresentCount
                                   where LocationSetCode=@locationSetCode AND   SerialNo =@SerialNo
                    
                                    --4
                                     
                                      select   @FSR= 'FSR-' + RIGHT('00' + CAST(right(Year(GETDATE()),2) AS CHAR(2)), 2 ) + RIGHT('00' + CAST(MONTH(GETDATE()) AS CHAR(2)), 2 ) +
                                       RIGHT('00000' + CAST((isnull(MAX(right(FSR_NO,5)),0) +1) AS NVARCHAR(5)), 5)   from TBVisitHD  
 
                                    --5 ยก
	                                    insert into TBVisitHD 
	                                    select @FSR,LocationSetCode,SerialNo,DateTimeIn,DateTimeOut,WorkType,PresentCount,
			                                    PreviousCount,TotalCount,ConversionRate,TotalNoOfRacks,Rent,Discount,
			                                    ServiceBy,getdate(),SignBy,@Status,IsComplain,Comment,CreateBy,getdate(),UpdateBy,getdate()
		                                from TBVisitHDTemp 
                                        where id =@Id

                                        insert into TBVisitDtlResult 
	                                    select @FSR,Seq,TitleResultName,SoilRemovalBit,FilmStreakBit,
                                               SpottedBit,ResultRemarkTh,ResultRemarkEng,Point
                                        from TBVisitDtlResultTemp
                                        where id =@Id

                                        insert into TBVisitDtlProcedure 
                                        select @FSR,Seq,TitleProcudureName,OkBit,ProcedureRemarkTh,
                                                 ProcedureRemarkEng,Point
                                        from TBVisitDtlProceduretemp                     
                                        where id =@Id

                                        insert into TBVisitDtlWashFactor 
                                        select @FSR,Seq,TitleWashFactorsName,Actual,
                                                 WashFactorRemarkTh,WashFactorRemarkEng,Point
                                        from TBVisitDtlWashFactorTemp
                                         where id =@Id

                                        insert into TBVisitDtlMechanical    
                                        select @FSR,Seq,TitleMechanicalName,OkBit,
                                                 MechanicalRemarkTh,MechanicalRemarkEng,Point
                                        from TBVisitDtlMechanicalTemp
                                         where id =@Id

                                        insert into TBVisitDtlMwwDispenser  
                                        select @FSR,Seq,TitleMwwDispenserName,ProbeBit,
                                                 DetTubeBit,RinseTubeBit,ConditionRemarkTh,
                                                 ConditionRemarkEng,Point
                                        from TBVisitDtlMwwDispenserTemp
                                         where id =@Id

                                        insert into TBVisitDtlFeeder
                                        select @FSR,Seq,TitleFeeDerName,FeederRemarkTh,
                                                FeederRemarkEng,Standard,Actual,Point
                                        from TBVisitDtlFeederTemp
                                         where id =@Id

                                        insert into TBVisitDtlTraining
                                        select @FSR,Seq,TitleOnsiteTraningName,OkBit
                                        from TBVisitDtlTrainingTemp
                                         where id =@Id

                                         insert into TBVisitDtlHandSoapDispenser
                                        select @FSR,Seq,TitleHandSoapDispenserName,
                                             Kitchen_Bit,HandSoapDispenserRemarkTh,
                                              HandSoapDispenserRemarkEng,Point
                                        from TBVisitDtlHandSoapDispenserTemp
                                         where id =@Id

                                    --6 delete
                                    Delete From TBVisitHDTemp where id =@Id
                                    Delete From TBVisitDtlResultTemp where id =@Id
                                    Delete From TBVisitDtlProceduretemp where id =@Id
                                    Delete From TBVisitDtlWashFactorTemp where id =@Id
                                    Delete From TBVisitDtlMechanicalTemp where id =@Id
                                    Delete From TBVisitDtlMwwDispenserTemp where id =@Id
                                    Delete From TBVisitDtlFeederTemp where id =@Id
                                    Delete From TBVisitDtlTrainingTemp where id =@Id
                                    Delete From TBVisitDtlHandSoapDispenserTemp where id =@Id
                                    ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Comment", access.VisitComment);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", access.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }

        //        public bool UpdateSignBy(AccessModels access)
        //        {
        //            bool result = true;
        //            string SQLCommand = @"  declare @Id int
        //                                    declare @FSR varchar(13)
        //                                    DECLARE @PresentCount int
        //                                      DECLARE @LocationSetCode varchar(10) 
        //                                     DECLARE @SerialNo varchar(20) 
        //
        //                                    select @Id= max(id) from TBVisitHDTemp 
        //                                    where createby =@UserID
        // 
        //                                    update TBVisitHDTemp
        //                                    SET SignBy=@SignBy,
        //                                        SignDateTime=GETDATE(),
        //	                                    UpdateBy=@UserId,
        //	                                    UpdateOn=GETDATE()	
        //                                    where Id =@Id 
        //
        //                                      SELECT @LocationSetCode =LocationSetCode
        //                                     ,@SerialNo =SerialNo ,
        //                                    @PresentCount=PresentCount
        //                                    from  tbvisithdtemp
        //                                     where Id =@Id
        //
        //    
        //                                      --2
        //                                    Update TBMachine
        //                                   set last_meter_no =@PresentCount
        //                                    where  SerialNo =@SerialNo
        //
        //                                    --3
        //                                    Update TBLocationMachineSet
        //                                    set total_meter_value =total_meter_value + @PresentCount
        //                                   where LocationSetCode=@locationSetCode AND   SerialNo =@SerialNo
        //                    
        //                                    --4
        //                                     
        //                                      select   @FSR= 'FSR-' + RIGHT('00' + CAST(right(Year(GETDATE()),2) AS CHAR(2)), 2 ) + RIGHT('00' + CAST(MONTH(GETDATE()) AS CHAR(2)), 2 ) +
        //                                       RIGHT('00000' + CAST((isnull(MAX(right(FSR_NO,5)),0) +1) AS NVARCHAR(5)), 5)   from TBVisitHD  
        // 
        //	                                     insert into TBVisitHD
        //	                                    select @FSR,LocationSetCode,SerialNo,DateTimeIn,DateTimeOut,WorkType,PresentCount,
        //			                                    PreviousCount,TotalCount,ConversionRate,TotalNoOfRacks,Rent,Discount,
        //			                                    ServiceBy,getdate(),SignBy,@Status,CreateBy,getdate(),UpdateBy,getdate()
        //		                                    from TBVisitHDTemp where id =@Id
        //    
        //                                    Delete From TBVisitHDTemp where id =@Id";

        //            using (var db = new DBHelper())
        //            {
        //                try
        //                {
        //                    db.BeginTransaction();
        //                    db.AddParameter("@SignBy", access.SignBy);
        //                    db.AddParameter("@Status", access.@Status);
        //                    db.AddParameter("@UserID", "900153");

        //                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
        //                    db.CommitTransaction();
        //                }
        //                catch (Exception ex)
        //                {
        //                    db.RollbackTransaction();
        //                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
        //                    return false;
        //                }
        //            }

        //            return result;
        //        }



        public bool Approve(AccessModels access)
        {
            bool result = true;
            string SQLCommand = @"
                                    update TBVisitHD
                                    SET SignBy=@SignBy,
                                        SignDateTime=GETDATE(),
                                        Status='Approve',
	                                    UpdateBy=@UserID,
	                                    UpdateOn=GETDATE()	
                                    where FSR_NO =@FSR_NO 

                                    ";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@FSR_NO", access.FSR_NO);
                    db.AddParameter("@SignBy", access.SignBy);
                    db.AddParameter("@UserID", access.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }

            return result;
        }
    }
}